$(function(){
	var istap = false;
	var isMenuShow = false;
	var nowIndex = 0;
	var isProduce = false;	//是否成功生成图片
	$(".img-list").on('touchstart touchmove touchend', 'img', function(e) {		
		switch(e.type) {
			case 'touchstart':				
				istap = true;
				break;
			case 'touchmove':
				istap = false;
				break;
			case 'touchend':
				if(istap) {
					var id = e.target.id;
					img[img.length] = new Canvas.Img(id, {});
					canvas1.addImage(img[img.length - 1]);
				}
				break;
		}
	});
	
	$(".handle-item").on('touchstart',function(e){		
		nowIndex = $(this).index()-1;
		$(this).addClass("active").siblings(".handle-item").removeClass("active");
		$(".img-list").eq(nowIndex).show().siblings(".img-list").hide();	
		$(".handle-arr>img").attr("src","img/arr_down.png");
		isMenuShow = true;
	});
	
	$(".handle-arr").on('touchstart',function(e){		
		isMenuShow = !isMenuShow;
		if(isMenuShow){
			$(this).find("img").attr("src","img/arr_down.png");
			$(".img-list").eq(nowIndex).show().siblings(".img-list").hide();
			$(".handle-item").eq(nowIndex).addClass("active").siblings(".handle-item").removeClass("active");
		}else{
			$(this).find("img").attr("src","img/arr_up.png");
			$(".img-list").hide();
			$(".handle-item").removeClass("active");
		}		
	});
	
	$(".e-btn").on('touchstart',function(e){		
		var iscomplete = false;
		if(canvas1._aImages && canvas1._aImages.length>0){
			canvas1._aImages[canvas1._aImages.length-1].bordervisibility = false;
			canvas1._aImages[canvas1._aImages.length-1].cornervisibility = false;
			canvas1.renderAll(false);
		}		
		var c1 = document.getElementById("canvid1");
		var c2 = document.getElementById("canvid1-canvas-container");
		c2.getContext("2d").drawImage(c1, 0, 0);
		var dataurl = c2.toDataURL();
		var targetimg = document.getElementsByClassName("s1-img")[0];
		targetimg.src = dataurl;
		$(".s1-container").show();
		
		var rimg = document.createElement("img");	
		html2canvas(document.getElementById("s1-box"),{
			onrendered: function(canvas){
				var imgurl = canvas.toDataURL();
				rimg.src = imgurl;							
			},
			useCORS: true
		});				
		$(rimg).appendTo($(".s1-opacity"));
		$(".s1-opacity").show();
	});
	
	
	$(".s1-right").on('touchstart',function(e){	
		$('.share-dialog').show();
	});
	
	$(".s1-left").on('touchstart',function(e){		
		$(".s1-container").hide();
		$('.s1-opacity').html(' ');
	});
	
	$('.share-dialog').on('touchstart',function(){
		$('.share-dialog').hide();
	});
	$('.share-img').on('touchstart',function(e){
		e.stopPropagation();
	})
	
	$(".e-close").on('touchstart',function(){
		var oimg = canvas1._aImages[canvas1._aImages.length-1];
		if(oimg.isselected){
			canvas1.removeImage(oimg);
		}
	});
	$(".e-lock").on('touchstart',function(){
		var oimg = canvas1._aImages[canvas1._oImgIndex];
		if(oimg.isselected){
			//判断是否锁定
			canvas1.lockImage(oimg);
			if(oimg.islocked){
				$(this).attr("src","img/sd_unlock.png");
			}else{
				$(this).attr("src","img/sd_lock.png");
			}
			canvas1.renderAll(false);
		}
	});
	$(".e-zd").on('touchstart',function(){
		var oImg = canvas1._aImages[canvas1._aImages.length-1];
		if(oImg.isselected){
			canvas1.toBottomImage(oImg);
		}		
	});
});



